package com.xiaoma.verbal.baseDao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xiaoma.tpo.baseDao.NameHandler;


public abstract class BaseDaoImpl<T> extends com.xiaoma.tpo.baseDao.BaseDaoImpl<T> {
	private static final Logger LOG = LoggerFactory.getLogger(BaseDaoImpl.class);
    /** 名称加工处理器 */
    private NameHandler    nameHandler;
    
    /** 具体操作的实体类对象 */
    private Class<T>       entityClass;
    
    /**
     * 构造方法，获取运行时的具体实体对象
     */
    @SuppressWarnings("unchecked")
    public BaseDaoImpl() {
        Type superclass = getClass().getGenericSuperclass();
        ParameterizedType type = (ParameterizedType) superclass;
        entityClass = (Class<T>) type.getActualTypeArguments()[0];
    }
	  /**
     * 获取实际运行时的名称处理器
     *
     * @return
     */
    private NameHandler getActualNameHandler() {
        if (nameHandler == null) {
            synchronized (this) {
                if (nameHandler == null) {
                    nameHandler = this.getNameHandler();
                }
            }
        }
        return nameHandler;
    }
	/**
	 * trunk 不支持事物所以改成 delete
	 */
	@Override
	public void deleteAll() {
        String tableName = this.getActualNameHandler().getTableName(entityClass.getSimpleName());
        String sql = " DELETE FROM " + tableName;
        LOG.info(sql);
        jdbcTemplate.execute(sql);
    }

}
